<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$mid	= intval(mosGetParam($_GET, 'm', 0));
$Itemid = intval(mosGetParam($_GET, 'Itemid', 0));

$midmatch = LM::getLMTable("Match");
if(!$midmatch->load($mid)){
	mosRedirect("index.php", "No such Match available");
}

$team1 = $midmatch->getTeam1();
$team2 = $midmatch->getTeam2();

$p = $midmatch->match_project;

$project = LM::getLMTable("Project");
if(!$project->load($p)){
	mosRedirect("index.php", "No such Project available");
}

$query1 = "SELECT m.*, tm1.teammap_id AS teammap1, tm2.teammap_id AS teammap2
			 FROM #__leagueman_match m, #__leagueman_teammap tm1, #__leagueman_teammap tm2
			WHERE match_team1 = " . $team1->getID() . "
			  AND match_team2 = " . $team2->getID() . "
			  AND tm1.teammap_project = m.match_project
			  AND tm1.teammap_team = " . $team1->getID() . "
			  AND tm2.teammap_project = m.match_project
			  AND tm2.teammap_team = " . $team2->getID() . "
			  AND published = 1";
	
$database->setQuery($query1);
$matches = $database->loadObjectList();
	
$query2 = "SELECT m.*, tm1.teammap_id AS teammap1, tm2.teammap_id AS teammap2
			 FROM #__leagueman_match m, #__leagueman_teammap tm1, #__leagueman_teammap tm2
			WHERE match_team2 = " . $team1->getID() . "
			  AND match_team1 = " . $team2->getID() . "
			  AND tm1.teammap_project = m.match_project
			  AND tm1.teammap_team = " . $team2->getID() . "
			  AND tm2.teammap_project = m.match_project
			  AND tm2.teammap_team = " . $team1->getID() . "
			  AND published = 1";
						  
$database->setQuery($query2);
	
$matches = array_merge($matches, $database->loadObjectList());

foreach($matches AS $match){
	$match->team1_name = ($match->match_team1 == $team1->getID()) ? $team1->getName() : $team2->getName();
	$match->team2_name = ($match->match_team2 == $team2->getID()) ? $team2->getName() : $team1->getName();
}

$mainframe->setPageTitle($team1->getName() . " vs. " . $team2->getName());

$template = LM::getTemplate();
$template->loadFilter("mambots");
	
$template->assign("project",	$project);
$template->assign("team1",		$team1);
$template->assign("team2",		$team2);
$template->assign("matches",	$matches);
$template->assign("match",		$midmatch);
$template->assign("Itemid",		$Itemid);

$template->display("matchinfo.tpl.php");

?>